#include <Windows.h>
#include <tchar.h>
#include "resource.h"
#include <locale.h>

INT_PTR CALLBACK DlgProc(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam)
{
	static HWND hNextViewer = NULL;
	switch (nMsg)
	{
		case WM_INITDIALOG:
		{
			hNextViewer = SetClipboardViewer(hWnd);
			HWND hFormat	= GetDlgItem(hWnd, IDC_STCFORMAT);
			HWND hContent	= GetDlgItem(hWnd, IDC_STCCONTENT);
			RECT rtFormat, rtContent;
			GetClientRect(hFormat, &rtFormat);
			GetClientRect(hContent, &rtContent);
			break;
		}	
		case WM_DRAWCLIPBOARD:
		{
			SetWindowText(hWnd, _T(""));
			if (OpenClipboard(hWnd))
			{
				if (IsClipboardFormatAvailable(CF_HDROP))
				{
					HDROP hDrop = (HDROP)GetClipboardData(CF_HDROP);
					UINT nCount = DragQueryFile(hDrop, -1, NULL, NULL);
					TCHAR szContent[4096] = {0};
					for (int ii = 0; ii < nCount; ii++)
					{
						TCHAR szPath[MAX_PATH] = {0};
						DragQueryFile(hDrop, ii, szPath, MAX_PATH);
						if (0 == _tcslen(szContent))
						{
							_tcscpy(szContent, szPath);
						} else {
							_tcscat(szContent, szPath);
						}
					}
					SetDlgItemText(hWnd, IDC_STCFORMAT, _T("Drop"));
					SetDlgItemText(hWnd, IDC_STCCONTENT, szContent);
				} else if (IsClipboardFormatAvailable(CF_TEXT))	{
					HANDLE hMem		= GetClipboardData(CF_TEXT);
					LPSTR szData	= (LPSTR)GlobalLock(hMem);
					UINT nSize		= GlobalSize(hMem);
					TCHAR* pContent = new TCHAR[(nSize + 1) * 2];
					memset(pContent, 0, sizeof(TCHAR) * nSize);
					setlocale(LC_CTYPE, "chs" );
					mbstowcs((wchar_t*)pContent, szData, strlen(szData));
					GlobalUnlock(hMem);
					
					SetDlgItemText(hWnd, IDC_STCFORMAT, _T("Text"));
					SetDlgItemText(hWnd, IDC_STCCONTENT, pContent);
					delete[] pContent;					
				} else if (IsClipboardFormatAvailable(CF_BITMAP)) {
					HBITMAP hBitmap	= (HBITMAP)GetClipboardData(CF_BITMAP);					
					HWND hCtrl		= GetDlgItem(hWnd, IDC_STCCONTENT);
					HDC hDC			= GetDC(hCtrl);
					HDC hMemDC		= CreateCompatibleDC(hDC);
					HGDIOBJ hGDIObj = SelectObject(hMemDC, hBitmap);
					RECT rtClient;
					GetClientRect(hCtrl, &rtClient);
					BITMAP bmp;
					GetObject(hBitmap, sizeof(BITMAP), &bmp);
					StretchBlt(hDC, 0, 0, rtClient.right - rtClient.left, rtClient.bottom - rtClient.top, hMemDC, 0, 0, bmp.bmWidth, bmp.bmHeight, SRCCOPY);
					SelectObject(hMemDC, hGDIObj);
					ReleaseDC(hCtrl, hDC);

					SetDlgItemText(hWnd, IDC_STCFORMAT, _T("Bitmap"));
				}
			}
			
			CloseClipboard();
			break;
		}
		case WM_SIZE:
		{
			int nWidth		= LOWORD(lParam);
			int nHeight		= HIWORD(lParam);
			RECT rtNew;
			GetWindowRect(hWnd, &rtNew);
			HWND hFormat	= GetDlgItem(hWnd, IDC_STCFORMAT);
			HWND hContent	= GetDlgItem(hWnd, IDC_STCCONTENT);
			RECT rtFormat, rtContent;
			GetWindowRect(hFormat, &rtFormat);
			GetClientRect(hContent, &rtContent);
			SetWindowPos(hFormat, NULL, 0, 0, rtNew.right - rtNew.left - 80, rtFormat.bottom - rtFormat.top, SWP_NOMOVE|SWP_NOACTIVATE);
			SetWindowPos(hContent, NULL, 0, 0, rtNew.right - rtNew.left - 30, rtNew.bottom - rtNew.top - 120, SWP_NOMOVE|SWP_NOACTIVATE);		
			break;
		}
		case WM_CLOSE:
		{
			ChangeClipboardChain(hWnd, hNextViewer);

			EndDialog(hWnd, 0);
		
			break;
		}
	}

	return 0;
}

BOOL WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE hPreInstance, LPTSTR szCmdLine, int nShowCmd)
{
	DialogBox(hInstance, MAKEINTRESOURCE(IDD_DIALOG), NULL, DlgProc);

	return TRUE;
}